import {Component, Input, OnInit } from '@angular/core';
import { ApiService } from '../../../../core/services/http/api.service';
import { FormGroup } from '@angular/forms';
import { FormlyFormOptions } from '@ngx-formly/core';
import { ApesDrawerRef } from '@shared/apes/drawer/apes-drawer-ref';

@Component({
  selector: 'data-kanban-drawer',
  templateUrl: './data-kanban-drawer.component.html',
  styleUrls: ['./data-kanban-drawer.component.scss'],
})
export class DataKanbanDrawerComponent implements OnInit {

  @Input() data: any;

  tableText: any = '无数据';

  /** formly 配置 **/
  ctx = { model: {} };
  form = new FormGroup({});
  formId = 'tms/dataKanban/dataKanbanDrawerComponent';
  model: any = {};
  options: FormlyFormOptions = {};
  field: any[] = [
    {
      key: 'branchLineId',
      type: 'span',
      templateOptions: {
        label: '线路名称',
        divCol: '6',
        valueFormatter: 'value.name',
      },
    },
    {
      key: 'carInformationId',
      type: 'span',
      templateOptions: {
        label: '派车单',
        divCol: '6',
      },
    },
    {
      key: 'chauffeurId',
      type: 'select',
      templateOptions: {
        searchFormId: 'tms.chauffeur.findAll',
        downFormatter: '\'[ \' + value.id + \' ]\' + \' \' + value.name',
        valueFormatter: '\'[ \' + value.id + \' ]\' + \' \' + value.name',
        label: '指派司机',
        api: 'tms.chauffeur.findAllByKanBan',
        disabled: true,
        divCol: '6',
      },
    },
    {
      key: 'vehicleId',
      type: 'select',
      templateOptions: {
        searchFormId: 'tms.vehicle.findAll',
        downFormatter: 'value.vehicleNumber + \' \' +  \'[ \' + value.vehicleStatus.description + \' ]\'',
        valueFormatter: 'value.vehicleNumber + \' \' +  \'[ \' + value.vehicleStatus.description + \' ]\'',
        label: '指派车辆',
        api: 'tms.vehicle.findAllByKanBan',
        disabled: true,
        divCol: '6',
      },
    },
  ];

  /** Grid 相关 **/
  rowData: any[] = [];

  constructor(
    private drawerRef: ApesDrawerRef<string>,
    private apiService: ApiService,
  ) {

  }

  ngOnInit() {
    this.initData();
  }


  initData() {
    let value = this.data;
    if (value) {
      this.model = {};
      this.form = new FormGroup({});
      this.rowData = [];
      this.tableText = '查询中';
    }

    let api = 'tms.waybill.findWaybillListByCarInformation';

    let data = {
      carInformation: value,
    };

    this.apiService.post(api, { 'body': JSON.stringify(data) }).subscribe((body) => {
      if (body) {
        this.model = body;
        this.rowData = body['waybillList'];
      } else {
        this.tableText = '无数据';
      }
    });
  }

  close(): void {
    this.drawerRef.close(this.formId);
  }

}
